package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OtherUserBean;
import com.koron.css2.systemManage.bean.PubSysUserPostBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.mapper.PostUserMapper;
import com.koron.css2.systemManage.service.IDataRangeService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Arrays;

public class OtherUserAdd  implements ServerInterface {
	private static Logger log = LogManager.getLogger(OrgUserAdd.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Void> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Void.class);
		try {
			OtherUserBean account = JsonUtils.objectToPojo(req.getData(), OtherUserBean.class);
			if(StringUtils.isEmpty(account.getCompanyNo())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("组织不能为空");
				return msg;
			}
			if(StringUtils.isBlank(account.getAccount())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("人员不能为空");
				return msg;
			}

			OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class, DBSourceUtils.getDbEnv(account.getCompanyNo()));
			IDataRangeService dataRangeService = ServiceFactory.getDataRangeService();
			OrgUserBean user = mapper.selectByAccount(account.getAccount());
			if(user == null) {
				account.setCreateInfo(userInfo);
				account.setPassword(Tools.MD5("krrj@2020"));
				account.setOpenId(Tools.getObjectId());
				int iCount = mapper.insertOther(account);
				if(iCount > 0) {
					dataRangeService.addUserRangeValue(ApplicationConfig.getAppCustomerAppid(), account.getAccount(), "company", Arrays.asList(account.getCompanyNo()));
				}
				log.info("已添加用户：{}", account.getUserName());
				
				PostUserMapper postUserMapper = factory.getMapper(PostUserMapper.class);
				postUserMapper.deleteUserPostByAcccount(account.getAccount());
				if(StringUtils.isNotEmpty(account.getPostNo())) {
					String postNo = account.getPostNo();
					String[] postArray = postNo.split(",");
					for(String post : postArray) {
						PubSysUserPostBean userPost = new PubSysUserPostBean();
						userPost.setCreateInfo(userInfo);
						userPost.setUserAccount(account.getAccount());
						userPost.setPostNo(post);
						postUserMapper.addUserPost(userPost);
					}
				}
				// IRoleService service = ServiceFactory.getRoleService();
				if(StringUtils.isNotEmpty(account.getRoleCode())) {
					String roleCode = account.getRoleCode();
					String[] roleArray = roleCode.split(",");
					//service.deleteRoleUser(account);
					for(String role : roleArray) {
						// service.addRoleUser(role, Arrays.asList(account));
					}
				}
			}else {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("账号已存在");
				return msg;
			}

		} catch (Exception e) {
			log.error("添加用户出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("添加用户出错");
		}
		return msg;
	}
}
